var password = document.getElementById("password");
var repeatPassword = document.getElementById("repeatPassword");

function displayPass() {
    if(password.value == ""){
        return;
    }
    var str = password.value;
    if(str.length < 8 || str.length > 16){
        var msg = document.getElementById("isRightPass");
        msg.setAttribute("class","newAccountFormErrorMsg");
        msg.innerText = "密码长度应该大于8并不超过16";
    }
    else{
        var msg = document.getElementById("isRightPass");
        msg.setAttribute("class","newAccountFormOkMsg");
        msg.innerText = "密码可以使用";
    }
}

function displayRepPass() {


    //更新界面可以不改密码，但是一旦改其中的一个就要对另外一个操作
    if (repeatPassword.value == "" && password.value != ""){
        var msg = document.getElementById("isRepeatPass");
        msg.setAttribute("class", "newAccountFormErrorMsg");//可以与注册界面的通用！
        msg.innerText = "请再次输入密码";
        return;
    }

    if(repeatPassword.value == ""){
        return;
    }

    if(repeatPassword.value != password.value){
        var msg = document.getElementById("isRepeatPass");
        msg.setAttribute("class","newAccountFormErrorMsg");//可以与注册界面的通用！
        msg.innerText = "请保证密码输入一致";
    }
    else{
        var msg = document.getElementById("isRepeatPass");
        msg.setAttribute("class","newAccountFormOkMsg");//可以与注册界面的通用！
    }

}

function changePass(){
    var msg = document.getElementById("isRightPass");
    msg.textContent = "";
}

function changeRepPass(){
    var msg = document.getElementById("isRepeatPass");
    msg.textContent = "";
}

password.onblur = displayPass;
password.oninput = changePass;
repeatPassword.onblur = displayRepPass;
repeatPassword.oninput = changeRepPass;

$(document).ready(function () {
    $("#editAccountForm").submit(function () {
        var msgPass = document.getElementById("isRightPass");
        var msgRepPass = document.getElementById("isRepeatPass");

        if(msgPass.className == "newAccountFormErrorMsg"){
            alert("密码格式错误");
            password.focus();
            return false;
        }
        else if(msgPass.className == "newAccountFormOkMsg" && msgRepPass.className == "" || msgRepPass.className == "newAccountFormErrorMsg"){
            alert("两次密码不一致!");
            repeatPassword.focus();
            return false;
        }

        return true;

    })
})